﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace LifeV2.Model
{
    public class World : INotifyPropertyChanged
    {
        public World(int width, int height)
        {
            Width = width;
            Height = height;

            var r = new Random();

            LifeMatrix = new List<ILifeForm>(Width * Height);
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    var creature = new Creature(this, i, j)
                                       {
                                           Health = r.Next(0, 100),
                                       };

                    LifeMatrix.Add(creature);
                }
            }
        }

        public int Width { get; set; }
        public int Height { get; set; }

        public IList<ILifeForm> LifeMatrix { get; set; }

        public ILifeForm this[int x, int y]
        {
            get
            {
                return LifeMatrix[10 * x + y];
            }
            set
            {
                LifeMatrix[10 * x + y] = value;
                RaisePropertyChanged("LifeMatrix");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void RaisePropertyChanged(string name)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}